package org.ccunix.eshop.controller.api;

import com.alibaba.fastjson.JSON;
import org.ccunix.eshop.domain.dto.MerchandiseVO;
import org.ccunix.eshop.service.MerchandiseServiceIface;
import org.ccunix.eshop.service.impl.MerchandiseServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/api/merchandise")
public class MerchandiseController extends HttpServlet {
    MerchandiseServiceIface merchandiseService = new MerchandiseServiceImpl();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("m");
        if ("getMerchantListBySpecial".equals(method)){
            getMerchantListBySpecial(request,response);
        }else if ("getMerchantById".equals(method)){
            getMerchantById(request,response);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
    //根据id查询商品详情
    private void getMerchantById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String id = request.getParameter("id");
        MerchandiseVO merchandiseVO = merchandiseService.getMerchantById(id);
        //写回去
        String data = JSON.toJSONString(merchandiseVO);
        response.getWriter().write(data);
    }
    //根据是否特价 查询商品集合
    public void getMerchantListBySpecial(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String special = request.getParameter("special");
        String query = request.getParameter("query");
        List<MerchandiseVO> list=list = merchandiseService.getMerchantListBySpecial(special,query);;
        String listJson = JSON.toJSONString(list);
        response.getWriter().write(listJson);
    }
}
